import { create } from 'zustand'
import { User } from '../types/api'
import storage from '../utils/storage'
export const useStore = create<{
  token: string
  userInfo: {
    userImg: string
    _id: string
    userId: number
    userName: string
    userEmail: string
    mobile: string
    deptId: string
    deptName: string
    job: string
    state: number
    role: number
    createId: number
    roleList: string
  }
  collapsed: boolean
  isDark: boolean
  updateUserInfo: (userInfo: User.UserItem) => void
  updateToken: (token: string) => void
  updateCollapsed: () => void
  updateTheme: (isDark: boolean) => void
}>(set => ({
  token: '',
  userInfo: {
    userImg: '',
    _id: '',
    userId: 0,
    userName: '',
    userEmail: '',
    mobile: '',
    deptId: '',
    deptName: '',
    job: '',
    state: 0,
    role: 0,
    createId: 0,
    roleList: ''
  },
  collapsed: false,
  isDark: storage.get('isDark') || false,
  updateUserInfo(userInfo: User.UserItem) {
    set({
      userInfo
    })
  },
  updateToken(token) {
    set({
      token
    })
  },
  updateTheme(isDark) {
    set({
      isDark
    })
  },
  updateCollapsed: () =>
    set(state => {
      return {
        collapsed: !state.collapsed
      }
    })
}))
